package com.tom.nio;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

/**
 * @author zx
 * @date 2024年09月30日 15:05
 */
public class TestNio2 {
    private static final String PAPTH = "C:\\Users\\86188\\Desktop\\springboot3.x-technology-tools\\netty\\netty-basic-01\\src\\main\\java\\com\\tom\\nio\\data.txt";

    public static void main(String[] args) {
        try (FileChannel channel = FileChannel.open(Path.of(PAPTH), StandardOpenOption.READ)) {
            ByteBuffer buffer = ByteBuffer.allocate(10);
            while (true) {
                int read = channel.read(buffer);
                if (read == -1) break;

                buffer.flip();// 切换为读模式
                while (buffer.hasRemaining()) {
                    byte b = buffer.get();
                    System.out.println("(char)b = " + (char) b);
                }
                buffer.clear();// 切换为写模式
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
